﻿
//经销商联系人
var columns =
            [
                { display: 'Key', name: 'Id', type: 'text', hide: 1 },
                { display: 'ClientId', name: 'ClientID', type: 'text', hide: 1 },
                 { display: '客户名称', name: 'ClientName', mintWidth: 80, width: 120 },
                { display: '联系人', name: 'LinkManNmae', mintWidth: 80, width: 120 },
                { display: '性别', name: 'SexName', mintWidth: 60, width: 80 },
                { display: '证件类型', name: 'CredentialsTypeName', mintWidth: 100, width: 100 },
                { display: '证件号码', name: 'CredentialsNo', mintWidth: 80, width: 100 },
                { display: '所在部门', name: 'Department', mintWidth: 80, width: 100 },
                { display: '职务', name: 'CurrentJob', mintWidth: 80, width: 100 },
                { display: '业务负责', name: 'BusinessScope', mintWidth: 80, width: 100 },
                { display: '手机', name: 'MobilePhone', mintWidth: 80, width: 100 },
                { display: '座机电话', name: 'StudioCamera', mintWidth: 80, width: 100 }
             ];

var manager;
//init controls
$(document).ready(function () {
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    initligerGrid();
});

function initligerGrid() {
    manager = null;
    manager = $("#ClitentLinksGrid").ligerGrid({
        columns: columns,
        title: '经销商联系人列表',//不进行gridview的行编辑，进行弹出，所以可以添加标题// 不能要标题，修改的BUG，不在ligerui.min.js文件中，所以这个BUG还是存在
        showTitle: false,
        width: '100%',
        height: '99%',
        enabledSort: true,
        //enabledEdit: true,
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                    { text: '增加', click: dialogAdd, icon: 'add' },
                                    { line: true },
                                    { text: '修改', click: modifyOperation, icon: 'modify' },
                                    { line: true },
                                    { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' },
                                    { line: true },
                                    { text: '查看', click: dialogDetailView, img: '../Content/images/Search.png' }
                                    ]
        },
        parms: { orgCode: $("#HidOrgCode").val(), ClientName: $("#ClientName").val(), LinkName: $("#LinkName").val() },
        url: "../Client/GetClientLinks?rand=" + Math.random(),
        onDblClickRow: function (data, rowindex, rowobj) { dialogView(data.Id); }
    });
}

//detail
function dialogDetailView() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择要查看的行!'); return; }
    var billId = row.Id.toString();
    dialogView(billId);
}
function dialogView(billId) {
    $.ligerDialog.open({ url: "../Client/LinkDetail?id=" + billId, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false });
}
//add
function dialogAdd() {
    var billId = $("#tempClientId").val();
    $.ligerDialog.open({ url: "../Client/AddLink?orgCode=" + $("#HidOrgCode").val()+"&Id="+ billId, height: getDialogHeight(), width: getDialogWidth(),
        isResize: true, isHidden: false
    });
}

//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要编辑的行!'); return;
    }

        $.ligerDialog.confirm('确定要删除吗？', function (yes) {
            if (yes) {

                $.get("../Client/DeleteLinks?clientLinkIds=" + row.Id.toString()+",", {}, function (data) {
                    if (data != undefined && data == "9") {
                        manager.deleteSelectedRow();
                        initligerGrid();

                    }
                });

            }
        });

}
//update 
function modifyOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要编辑的行!'); return;
    }
    $.ligerDialog.open({ url: '../Client/AddLink?type=edit&Id=' + row.Id, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}



//======================================================
function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['Key']);
        else
            removeCheckedCustomer(this.records[rowid]['Key']);
    }
}
/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/

var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.CustomerID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.Id);
    else removeCheckedCustomer(data.Id);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
